package de.hauck.rtmidlet.views;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

import de.hauck.rtmidlet.FileExImportInterface;
import de.hauck.rtmidlet.FileSystemExportThread;
import de.hauck.rtmidlet.FileSystemImportThread;
import de.hauck.rtmidlet.RTMidlet;
import de.hauck.rtmidlet.model.Configuration;
import de.hauck.rtmidlet.utils.FileChooser;
import de.hauck.rtmidlet.utils.MyLogger;

public class FormConfig extends Form implements CommandListener,FileExImportInterface {

	private String _importFile;
	private FormSettingsMenu _fm;
	private Command cSave = new Command("Save", Command.OK, 1);
	private Command cCancel = new Command("Cancel", Command.BACK, 1);
	private Command cExportConfig = new Command("Export Config", Command.BACK,2);
	private Command cImportConfig = new Command("Import Config", Command.BACK,3);
	
	private Command cImportYes = new Command("Yes", Command.OK, 1);
	private Command cImportNo = new Command("No", Command.BACK, 1);
	
	private Command cConfirmOK = new Command("OK", Command.OK, 1);
	private TextField timeOffset,exportPathCSV,IBtoken;
	
	private TextField[] tf_IBqueries=new TextField[Configuration.IB_QUERIES.length];
	
	public FormConfig(FormSettingsMenu fm) {
		super("Configuration");
		//_main=rtmidlet;
		_fm=fm;
		
		timeOffset=new TextField("TimeZone", Configuration.timeOffset, 2, TextField.NUMERIC);
		IBtoken=new TextField("IB Token", Configuration.IB_TOKEN, 30, TextField.ANY);
		//IBquery=new TextField("IB Query", Configuration.IB_QUERY_TODAY, 6, TextField.NUMERIC);
		exportPathCSV=new TextField("Export path", Configuration.exportPath, 255, TextField.ANY);
		append(timeOffset);
		append(IBtoken);
		//append(IBquery);
		System.out.println("ibquery length: "+tf_IBqueries.length);
		for (int i=0; i<tf_IBqueries.length; i++) {
			tf_IBqueries[i]=new TextField("IB Query "+i, Configuration.IB_QUERIES[i],8,TextField.NUMERIC);
			append(tf_IBqueries[i]);
		}
		append(exportPathCSV);
				
		addCommand(cSave);
		addCommand(cCancel);
		addCommand(cExportConfig);
		addCommand(cImportConfig);
		setCommandListener(this);
		RTMidlet.getInstance().show(this);		
	}

	public void commandAction(Command command, Displayable dble) {
		if (command==cSave) {
			Configuration.dirtyFlagConfig=true;
			MyLogger.debug("Setting Link settings");
			Configuration.timeOffset=timeOffset.getString();
			Configuration.IB_TOKEN=IBtoken.getString();
			//Configuration.IB_QUERY_TODAY=IBquery.getString();
			String[] IBQueryStrings = new String[tf_IBqueries.length];
			for (int i=0; i<tf_IBqueries.length; i++) {
				IBQueryStrings[i]=tf_IBqueries[i].getString();
			}
			Configuration.IB_QUERIES=IBQueryStrings;
			Configuration.exportPath=exportPathCSV.getString();
			RTMidlet.getInstance().show(_fm.getSettingsMenu());
		} else if (command==cExportConfig) {
			if (Configuration.exportPath!=null) {
				new FileSystemExportThread(this,2);
			} else RTMidlet.getInstance().alert("Info", "No path to export selected", Configuration.ALERT_WAIT_TIME);
		} else if (command==cImportConfig) {
			FileChooser fchoser = new FileChooser(null,true,this);
			RTMidlet.getInstance().show(fchoser);
		} else if (command == cImportYes) {
			new FileSystemImportThread(1, _importFile, this);
			Configuration.dirtyFlagStocks=true;
			RTMidlet.getInstance().show(this);
		} else if (command == cImportNo) {
			RTMidlet.getInstance().show(this);
		} else if (command == cCancel) {
			RTMidlet.getInstance().show(_fm.getSettingsMenu());
		} else if (command == cConfirmOK) {
			// to refresh data on new call
			RTMidlet.getInstance().show(_fm.getSettingsMenu());
		}	
		
	}

	public void showExportSuccess() {
		Alert export_confirm = new Alert(
				"Export",
				"Export was successfull.",
				null, AlertType.INFO);
		export_confirm.addCommand(cConfirmOK);
		export_confirm.setCommandListener(this);
		RTMidlet.getInstance().show(export_confirm);
		
	}

	public void showImportSuccess() {
			Alert import_confirm = new Alert(
					"Import",
					"Import was successfull.",
					null, AlertType.INFO);
			import_confirm.addCommand(cConfirmOK);
			import_confirm.setCommandListener(this);
			RTMidlet.getInstance().show(import_confirm);
		
	}
	
	public void showImportConfirmation(String file) {
		Alert import_confirm = new Alert(
				"Import",
				"Do you really want to import "+file+"? Stored config will be deleted!",
				null, AlertType.CONFIRMATION);
		import_confirm.addCommand(cImportYes);
		import_confirm.addCommand(cImportNo);
		import_confirm.setCommandListener(this);	
		RTMidlet.getInstance().show(import_confirm);
		_importFile=file;
	}
	
	public void showConfig() {
		RTMidlet.getInstance().show(this);
	}

}
